#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/fb.h>
#include <linux/un.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include "common.h"
#include "cJSON.h"
#include "head.h"

static char req[1024*1024*2+500]; 

/**
 * @brief http请求组包
 * @param  pic              My Param doc
 * @return char* 
 */
char *http_request(char *pic)
{
	bzero(req, 1024*1024*2+500);
	snprintf(req,1024*1024*2+500, "POST /ocr/car-license?pic=%s HTTP/1.1\r\n"
			   "Host: api03.aliyun.venuscn.com\r\n"
			   "Authorization: APPCODE 431922883a3f432a96f4d57e3a05fa7c\r\n\r\n",
			   strtok(pic, "\n"));
			 
	return req;
}


/**
 * @brief 主函数
 * @param  argc             My Param doc
 * @param  argv             My Param doc
 * @return int 
 */
int main(int argc, char **argv)
{
    //打开有名管道
    int fifo_BASE64_CARID = open(BASE64_CARID, O_RDWR);
	int fifo_SQLite_CARID = open(SQLite_CARID, O_RDWR);

    char *pic = calloc(1,1024*1024*2);

    read(fifo_BASE64_CARID,pic,1024*1024*2);

    // 1，取得服务器的IP
	struct hostent *he = gethostbyname("api03.aliyun.venuscn.com");
	if(he == NULL)
	{
		printf("获取服务器IP失败: %s\n", strerror(h_errno));
		exit(0);
	}
	char *ip = inet_ntoa(*((struct in_addr *)(he->h_addr_list[0])));

    // 2，试图连接服务器
	int sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	struct sockaddr_in addr;
	socklen_t len = sizeof(addr);
	bzero(&addr, len);

	addr.sin_family = AF_INET;
	addr.sin_addr = *((struct in_addr *)(he->h_addr_list[0])); //ip地址
	addr.sin_port = htons(80); // http端口号

	Connect(sockfd, (struct sockaddr *)&addr, len);
	printf("连接成功！\n");


    // 3.构造http报文
    char *req = http_request(pic);
    int len_req = strlen(req);
    int m;
    while(len_req > 0)
    {
        m = write(sockfd, req, strlen(req));
        len_req -= m;
        req += m;
    }
    printf("发送请求报文成功！\n");

    // 4.等待服务器回应
    char response[1024];
    bzero(response, 1024);

    // 4.1 读取响应头部
    for(int i=0; ; i++)
    {
        read(sockfd, response+i, 1);

        if(strstr(response, "\r\n\r\n"))
            break;
    }

    // 4.2 读取JSON
    int len_json = get_size(response);
    printf("JSON数据长度:%d\n", len_json);

    char *json = calloc(1, len_json);
    int total = 0;
    while(len_json > 0)
    {
        int n = read(sockfd, json+total, len_json);
        len_json -= n;
        total += n;
    }
    printf("JSON:\n%s", json);

    // 5，分析JSON数据
    cJSON *root = cJSON_Parse(json);
    cJSON *data = cJSON_GetObjectItem(root, "data");
    char *number = cJSON_GetObjectItem(data, "number")->valuestring;	

    printf("\n\n");
    printf("识别的车牌: %s\n", number);

    // 6.将识别出来的车牌号发送给数据库程序
    int n = write(fifo_SQLite_CARID,number,strlen(number));

    exit(0);
}